//
//  WJNetworkingTools.swift
//  WJWeiBo
//
//  Created by wangjun on 16/7/22.
//  Copyright © 2016年 wangjun. All rights reserved.
//

import UIKit
import AFNetworking
//请求类型
enum requestType :String {
    case POST = "POST"
    case GET = "GET"
}

class WJNetworkingTools: AFHTTPSessionManager {

    static let shareNetworkingTools: WJNetworkingTools = {
        let tools = WJNetworkingTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    
    }()
    
    func requestData(type: requestType,urlStr: String,parameters: AnyObject?,success: (response:AnyObject?)->(),failure: (error: NSError)->()) {
        //判断请求类型
        if type == .GET {
            
            GET(urlStr, parameters: parameters, progress: nil, success: { (_,res) -> Void in
                success(response: res)
                }, failure: { (_, err) -> Void in
                    failure(error: err)
            })
            
        }else{
            
            POST(urlStr, parameters: parameters, progress: nil, success: { (_,res) -> Void in
                success(response: res)
                }, failure: { (_, err) -> Void in
                    failure(error: err)
        })
        }
        
    }
    
    
}


//MARK : 请求OAuth界面数据
extension WJNetworkingTools {
    
    //userAcount
    func loadUserAcount(code: String,success: (response:AnyObject?)->(),failure: (error: NSError)->()) {
    
    let urlStr: String = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = ["client_id":WJAPPKEY,
            "client_secret":WJSECRET,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":BACK_URL]
        
        requestData(requestType.POST, urlStr: urlStr, parameters: parameters, success: success, failure: failure)
 
    }
    
    
   //MARK : 请求用户信息数据
    func loadUserInfo(userAccount: WJUserAccount, success: (response:AnyObject?)->(),failure: (error: NSError)->()){
        
        let userInfoUrl = "https://api.weibo.com/2/users/show.json"
        
        let param = [
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid!
        ]
        
        WJNetworkingTools.shareNetworkingTools.GET(userInfoUrl, parameters: param, progress: nil, success: { (_, response) -> Void in
            
            guard let res = response as? [String :AnyObject]  else {
                
                return
            }
            
            //根据传过来的模型来赋值
            userAccount.avatar_large = res["avatar_large"] as! String
            userAccount.screen_name = res["screen_name"] as! String
            
            }) { (_, error) -> Void in
                
                print(error)
        }
        
    }
    
    

}
  //MARK : 请求微博首页信息
extension WJNetworkingTools {
    func homeLoadData(sinceId: Int64, maxId: Int64,success: (response: AnyObject?)->(), failure: (error: NSError)->()){
        // 请求urlString
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        // 请求参数
        let params = ["access_token": WJUserAccountViewModel.shareTools.accessToken!,
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)"
        ]
        requestData(requestType.GET, urlStr: urlString, parameters: params, success: success, failure: failure)
        
    }
}
    

